package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.GradeCursoDAO;
import br.urca.model.GradeCurso;
import br.urca.model.GradeCursoPK;

@Transactional
public class JPAGradeCursoDAO implements GradeCursoDAO {
	
	private EntityManager manager;
	
	public JPAGradeCursoDAO() {
		this(null);
	}
	
	@Inject
	public JPAGradeCursoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<GradeCurso> todos() {
		try {
			return this.manager.createQuery("SELECT g FROM GradeCurso g",
					GradeCurso.class).getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public GradeCurso getGradePorId(GradeCursoPK id) {
		try {
			return this.manager.createQuery("SELECT g FROM GradeCurso g "
					+ "WHERE g.id.codigoCurso = :curso "
					+ "AND g.id.codigoHabilitacao = :habilitacao "
					+ "AND g.id.grade = :grade "
					+ "AND g.id.turma = :turma ",
					GradeCurso.class)
					.setParameter("curso", id.getCodigoCurso())
					.setParameter("habilitacao", id.getCodigoHabilitacao())
					.setParameter("grade", id.getGrade())
					.setParameter("turma", id.getTurma())
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

}
